import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

public class DoublyLinkedListSentinel implements Iterable<Integer> {

    /**
     * 内部类
     */
    private static class Node {
        Node prev;
        int value;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    private final Node sentinel = new Node(null,-1,null);

    /**
     * 构造{@code sentinel-哨兵}环形链表
     */
    public DoublyLinkedListSentinel() {
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
    }

    /**
     * 头插法
     * @param value 插入值
     */
    public void addFirst(int value) {
        Node prev = sentinel;
        Node next = sentinel.next;
        Node added = new Node(prev,value,next);
        prev.next = added;
        next.prev = added;
    }

    /**
     * 尾插法<br>
     * @param value 插入值
     */
    public void addLast(int value) {
        Node prev = sentinel.prev;
        Node next = sentinel;
        Node added = new Node(prev,value,next);
        prev.next = added;
        next.prev = added;
    }

    /**
     * 头删法
     */
    public void removeFirst() {
        Node removed = sentinel.next;
        if (removed == sentinel) {
            throw new IllegalArgumentException("非法");
        }
        Node prev = sentinel;
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;
    }

    /**
     * 尾删法
     */
    public void removeLast() {
        Node removed = sentinel.prev;
        if (removed == sentinel) {
            throw new IllegalArgumentException("非法");
        }
        Node prev = removed.prev;
        Node next = sentinel;
        prev.next = next;
        next.prev = prev;
    }

    /**
     * 值删法
     * @param value
     */
    public void removeByValue(int value) {
        Node curr = findNodeByValue(value);
        if (curr != null) {
            Node prev = curr.prev;
            Node next = curr.next;
            prev.next = next;
            next.prev = prev;
        }else {
            throw new IllegalArgumentException("非法");
        }
    }

    /**
     * 由值查找节点
     * @param value
     * @return
     */
    private Node findNodeByValue(int value) {
        Node curr = sentinel.next;
        while (curr != sentinel) {
            if (curr.value == value) {
                return curr;
            }
            curr = curr.next;
        }
        return null;
    }

    /**
     * 迭代器遍历
     * @return
     */
    @Override
    public @NotNull Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node curr = sentinel.next;
            @Override
            public boolean hasNext() {
                return curr != sentinel;
            }

            @Override
            public Integer next() {
                int value = curr.value;
                curr = curr.next;
                return value;
            }
        };
    }
}
